<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
	<%@page import="com.framework.entity.Global"%>
<%@ page import="com.biz.utils.ConfigUtil" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
    String resoursUrl = ConfigUtil.get("RESOURCES_URL");
%>
<!DOCTYPE html>
<html class="no-js">
<head>
  <meta charset="UTF-8">
  <title>购物车</title>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="format-detection" content="telephone=no">
  <meta name="renderer" content="webkit">
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <jsp:include page="/jsp/inc_head.jsp"></jsp:include>
<style>
	.jlqspanorange{
		background-color:#ff9f09;
		color:#fff;
		border-radius:4px;
		display:inline-block;
		padding:0 3px;
		font-size:12px;
	}
	.am-padding-right-0{
		padding-right:0;
	}
	.am-padding-0{
		padding-left:0;
		padding-right:0;
	}
</style>
</head>
<body>
<section class="am-g">
    <div class="am-u-sm-12 am-padding-0"  id="cartlist">
       
    </div>
</section>

<!--底部合计-->
<div class="am-g gdDbdiv"></div>
<div class="footeNavbar gwcBtHjjs">
	<div class="am-u-sm-2 am-padding-right-0 am-text-left" style="width:15%">
    	<label><input type="checkbox" style="margin-top:2px;" onclick="saveAll()" name="cartAll" > 全选</label>
    </div>
    <div class="am-u-sm-7 am-padding-0">合计：<span class="am-text-sm colred" id="cartTotalMoney">￥0.00</span> <span class="am-text-xs coleee" id="fwf">服务费0元</span></div>
    <a class="am-u-sm-3 am-padding-0 spxqljgmBtn" id="shopCarCount" onclick="toSettle();" >结算（0）</a>
</div>

<!--footer-->
<jsp:include page="/jsp/inc_bottom.jsp"></jsp:include>
<!--footer-->
<div class="am-modal am-modal-alert" tabindex="-1" id="my-alert">
	<div class="am-modal-dialog">
		<div class="am-modal-hd" id="alert_title">哎呦喂，出错啦</div>
		<div class="am-modal-bd" id="alert_message"></div>
		<div class="am-modal-footer">
			  <span class="am-modal-btn">确定</span>
		</div>
	</div>
</div>
<script>
var ajaxpath="${pageContext.request.contextPath}";
var shopIdList=[];//shopId列表
var cartIdList=[];//cartIdList列表
$(function(){
	querycartList();
});

function querycartList(){
		var url = ajaxpath+"/cart/getOrderCartList.do?guid="+ new Date();
		$.get(url, function(data) {
		     var html="";
		     $.each(data,function(i){
		          shopIdList[i]=data[i].shopId;
		          cartIdList[i]=data[i].id;
		          var typename="";
		          if(data[i].typename1!=""&&data[i].typename1!=null){
		               if(typename==""){
		                    typename+=data[i].typename1;
		               }else {
		                    typename+=","+data[i].typename1;
		               }
		          }
		          if(data[i].typename2!=""&&data[i].typename2!=null){
		               if(typename==""){
		                    typename+=data[i].typename2;
		               }else {
		                    typename+=","+data[i].typename2;
		               }
		          }
		          if(data[i].typename3!=""&&data[i].typename3!=null){
		               if(typename==""){
		                    typename+=data[i].typename3;
		               }else {
		                    typename+=","+data[i].typename3;
		               }
		          }
		          html+='<div class="am-u-sm-12 qrddWwdiv" id="buycarlist'+data[i].id+'">'+
		                '<div class="am-u-sm-1 am-padding-0 am-margin-top"><input onclick="totalMoney();" type="checkbox" value="'+data[i].id+'" name="cart"></div>'+
		                '<div class="am-u-sm-3 am-padding-0">'+
		                '<img onclick="toGoodsDetail(\''+data[i].goodsId+'\',\''+data[i].id+'\');" ' ;
				 if(data[i].mainType =='3')
				 {
					 html+='src="<%=Global.OSSURL_MAINTYPE3%>/'+data[i].path+'" class="sptpimg" />';
				 }else{
					 html+='src="<%=Global.OSSURL%>/'+data[i].path+'" class="sptpimg" />';
				 }
				  html+='</div>'+
		                '<div class="am-u-sm-8 am-padding-0" style="position:relative">'+
		                '<div class="am-u-sm-12 am-padding-0" id="listcontent'+data[i].id+'">'+
		                '<div class="am-u-sm-10 am-padding-0">'+
		                '<div class="am-u-sm-12 am-padding-0 am-text-truncate" onclick="toGoodsDetail(\''+data[i].goodsId+'\',\''+data[i].id+'\');" >'+data[i].name+'</div>'+
		                '<div class="am-u-sm-12 infogg">品种：'+typename+' &nbsp</div>';

if(data[i].isTicket==1)
{html+=   '<div class="am-u-sm-12 am-padding-0 colred">￥'+data[i].price+'</div>';}
else
{html+=   '<div class="am-u-sm-12 am-padding-0 colred"><span class="jlqspanorange">久零券</span><span class="colred11">'+data[i].price+'券</span></div>';}




				 html+=   '</div>'+
		                '<div class="am-u-sm-2 am-padding-0 am-text-right">'+
		                '<div class="am-u-sm-12 am-padding-0" style="margin-top:40px;" id="showbuycount'+data[i].id+'">X'+data[i].count+'</div>'+
		                '</div>'+
		                '</div>'+
		                '<div class="gwcbjicon" id="gwcbjicon'+data[i].id+'" onclick="edit(\''+data[i].id+'\');" ><img src="<%=resoursUrl%>resources/assets/images/gwcbjicon.png" width="24" /></div>'+
		                '<div class="am-u-sm-12 am-padding-0" id="listset'+data[i].id+'" style="display:none;">'+
		                '<div class="am-u-sm-10 am-padding-0">'+
		                '<div class="am-u-sm-12 minMaxDiv">'+
		                '<div class="am-u-sm-3 minplstyleO" id="minus_cart'+data[i].id+'" onclick="minus(\''+data[i].id+'\');">'+
		                '<i class="am-icon-minus"></i>'+
		                '</div>'+
		                '<div class="am-u-sm-6 am-padding-0" style="height:27px;">'+
		                '<input type="text" class="buycountO" value="'+data[i].count+'" onchange="changecount(\''+data[i].id+'\');" id="buycount'+data[i].id+'" readonly="readonly">'+
		                '</div>'+
		                '<div class="am-u-sm-3 maxplstyleO" id="plus_cart'+data[i].id+'" onclick="plus(\''+data[i].id+'\');">'+
		                '<i class="am-icon-plus"></i>'+
		                '</div>'+
		                '</div>'+
		                '<div class="am-u-sm-12 infogg">品种：'+typename+' &nbsp</div>'+
		                '</div>'+
		                '<div class="am-u-sm-2 listSetBtn" style="width:30%">'+
		                '<div class="am-u-sm-12 am-padding-0 listsetok" id="listsetok'+data[i].id+'" onclick="complete(\''+data[i].id+'\');" >完成</div>'+
		                '<div class="am-u-sm-12 am-padding-0 listsetdel" id="listsetdel'+data[i].id+'" onclick="deletedata(\''+data[i].id+'\');" >删除</div>'+
		                '</div>'+
		                '</div>'+
		                '</div>'+
		                '<input id="formercount'+data[i].id+'" type="hidden" value="'+data[i].count+'"/>'+
		                '<input id="price'+data[i].id+'" type="hidden" value="'+data[i].price+'"/>'+
		                '</div>';      
		     });
		     $("#cartlist").html(html);
			var checkbox1 = document.getElementsByName('cartAll');
			 checkbox1[0].checked="checked";//全选按钮当前状态
			saveAll();
		     totalMoney();
		});
}

//编辑
function edit(id){
      $("#buycarlist"+id).addClass("backeee");
      $("#gwcbjicon"+id).hide();
      $("#listcontent"+id).hide();
      $("#listset"+id).show();
}

//删除
function deletedata(id){
      var url=ajaxpath+"/cart/deleteOrderCart.do?id="+id;
      $.get(url, function(data) {
		       if(data.result=="success"){
		       		$("#buycarlist"+id).remove();
		       		totalMoney();
		       }else {
		            $("#alert_message").html("删除失败！");
		            $("#my-alert").modal('open');
		       }
		  });
}

//完成 
function complete(id){
      var formercount=$("#formercount"+id).val();
      var buycount=$("#buycount"+id).val();
      if(formercount==buycount){
	      $("#buycarlist"+id).removeClass("backeee");
		  $("#listset"+id).hide();
		  $("#listcontent"+id).show();
		  $("#gwcbjicon"+id).show();
      }else {
          var url=ajaxpath+"/cart/saveOrderCartchange.do?id="+id+"&count="+buycount;
		  $.get(url, function(data) {
		       if(data.result=="success"){
		       		$("#formercount"+id).val(buycount);
		       		$("#showbuycount"+id).text("X"+buycount);
		       		$("#buycarlist"+id).removeClass("backeee");
		            $("#listset"+id).hide();
		            $("#listcontent"+id).show();
		            $("#gwcbjicon"+id).show();
		            totalMoney();
		       }else if(data.result=="numErr"){
				   $("#alert_message").html("修改失败！库存不足");
				   $("#my-alert").modal('open');
				   $("#buycount"+id).val(data.num);
			  }else {
		            $("#alert_message").html("修改失败！");
		            $("#my-alert").modal('open');
		            $("#buycount"+id).val(formercount);
		            $("#buycarlist"+id).removeClass("backeee");
		            $("#listset"+id).hide();
		            $("#listcontent"+id).show();
		            $("#gwcbjicon"+id).show();
		       }
		  });
      }  
}

//跳转商品详情页
function toGoodsDetail(goodsid,cartid){
    window.location.href=ajaxpath+"/client_toGoodsdetail.do?goodsid="+goodsid+"&stype="+1+"&cartid="+cartid;
}

//加
function plus(id){
     var now_num=$("#buycount"+id).val();
     $("#buycount"+id).val(parseInt(now_num)+1);
}

//减
function minus(id){
     var now_num=$("#buycount"+id).val();
	 if(now_num==1){
		 return false;
	 }else{
		 $("#buycount"+id).val(parseInt(now_num)-1);
     }
}

//修改
function changecount(id){
	 var re = /^[0-9]*[1-9][0-9]*$/ ;
     var now_num=$("#buycount"+id).val();
	 if(!re.test(now_num)){	
	    $("#alert_message").html("请填写正整数！");
		$("#my-alert").modal('open');	
		$("#buycount"+id).val(1);	
	}
	 if(now_num<1){
	     $("#alert_message").html("数量不能小于1！");
		 $("#my-alert").modal('open');
		 $("#buycount"+id).val(1);
	 } 
}

 /**
 * 计算选中购物车中选中商品的总计金额与清单数量
 */
 function totalMoney()
 {
     var j=0;//被选中的购物车清单数量
	 var checkbox = document.getElementsByName('cart');
	 var totalMoney = 0;
	 for (var i=0;i<checkbox.length;i++)
	 {
		 //被选中
		 if (checkbox[i].checked==true)
		 {
             j++;
			 var id=checkbox[i].value;
			 var price=$("#price"+id).val();
			 var count=$("#formercount"+id).val();
			 totalMoney += accMul(price,count);
		 }
	 }
     $("#shopCarCount").html("结算（"+j+"）");
	 $("#cartTotalMoney").html(''+totalMoney.toFixed(2)+'券');
	 var fwf=totalMoney*0.03;
	 $("#fwf").html("服务费"+fwf.toFixed(2)+"元");
}

 /**
 * 全选与全不选
 */
 function saveAll()
 {
     var checkbox1 = document.getElementsByName('cartAll');
     var checkedall = checkbox1[0].checked;//全选按钮当前状态
     
	 var checkbox = document.getElementsByName('cart');
	//将所有的按钮状态设置成与全新按钮一样的状态
    for(var i=0;i<checkbox.length;i++){
         checkbox[i].checked = checkedall;
    }
    totalMoney();
}

//去结算
function toSettle(){
    var selectall=true;//判断是否全选
    var flag=true;//判断是否有两个不同店家
    var checkbox = document.getElementsByName('cart');
    var cartId="";
    var shopIdcompare="";
    var comparecount=0;
    for(var i=0;i<checkbox.length;i++){
        if(checkbox[i].checked==true){
             if(comparecount==0){
                 shopIdcompare=shopIdList[i];
                 comparecount++;
             }else{
                 if(shopIdcompare!=shopIdList[i]){
                     flag=false;
                 }
             }
             if(cartId==""){
                 cartId+=cartIdList[i];
             }else{
                 cartId+=","+cartIdList[i];
             }  
        }else{
            selectall=false;
        } 
    }
    if(!flag){
         $("#alert_message").html("一次只能支付一家店铺商品！");
		 $("#my-alert").modal('open');
    }else if(cartId.length<=0)
	{ $("#alert_message").html("请选择去结算的商品");
		$("#my-alert").modal('open');}
    else{
		var param='';
		if (selectall) {
			param="type=0" ;
		}
		else
		{param="type=1&cartid=" + cartId;}
		var url=ajaxpath+"/cart/checkStock.do?"+param;
		$.get(url, function(data) {
		if(data.flag=="true")
		{window.location.href = ajaxpath + "/order/toOrderConfirm.do?"+param;}
		else
		{  $("#alert_message").html(data.names+" 库存不足！");
			$("#my-alert").modal('open');}
		});
    }
}

//浮点数运算加法
function accAdd(arg1,arg2){  
    var r1,r2,m;

    try {
        r1 = arg1.toString().split(".")[1].length
    } catch (e) {
        r1 = 0
    }
    try {
        r2 = arg2.toString().split(".")[1].length
    } catch (e) {
        r2 = 0
    }
    m=Math.pow(10,Math.max(r1,r2));  
    return (arg1*m+arg2*m)/m  ;
}
//浮点数运算减法
function accSub(arg1,arg2){      
    return accAdd(arg1,-arg2);  
}
//浮点数运算乘法
function accMul(arg1,arg2)  
{  
    var m=0,s1=arg1.toString(),s2=arg2.toString();  
    try{m+=s1.split(".")[1].length}catch(e){}  
    try{m+=s2.split(".")[1].length}catch(e){}  
    return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m) ; 
}
//浮点数运算除法
function accDiv(arg1,arg2){  

    var t1=0,t2=0,r1,r2;  
    try{t1=arg1.toString().split(".")[1].length}catch(e){}  
    try{t2=arg2.toString().split(".")[1].length}catch(e){}  
	
    with(Math){  
        r1=Number(arg1.toString().replace(".","")) ; 
        r2=Number(arg2.toString().replace(".","")) ;

        return (r1/r2)*pow(10,t2-t1);  
    }  
}

function toDecimal2(x) {  
            var f = parseFloat(x);  
            if (isNaN(f)) {  
                return false;  
            }  
            var f = Math.round(x*100)/100;  
            var s = f.toString();  
            var rs = s.indexOf('.');  
            if (rs < 0) {  
                rs = s.length;  
                s += '.';  
            }  
            while (s.length <= rs + 2) {  
                s += '0';  
            }  
            return s;  
}  
</script>

</body>
</html>
